//
//  NewFeatureViewController.swift
//  sinaBlog
//
//  Created by tianxiangyang on 15/12/16.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

import UIKit
import SnapKit


let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {

    
    init(){
    
      let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = CGSizeMake(ScreenWidth, ScreenHeight)
        
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        layout.minimumLineSpacing = 0
        
        layout.minimumInteritemSpacing = 0
        
      super.init(collectionViewLayout: layout)
    
    
        collectionView?.bounces = false
        
        collectionView?.pagingEnabled = true
        
        collectionView?.showsHorizontalScrollIndicator = false
    
    
    
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return 4
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.index = indexPath.item
    
        if cell.index == 3 {
        
            cell.startAnimation()
        }
        
        
        return cell
    }

  

}

//MARK:自定义cell

class NewFeatureCell:UICollectionViewCell{
    
    var index:Int  = 0 {
    
        didSet{
            
         backImageView.image = UIImage(named: "new_feature_\(index + 1)")
            
//        startBtn.hidden = index != 3
        
            startBtn.hidden = true
            
        }
    
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //MARK:设置UI界面
    
    func setUpUI(){
    
       contentView.addSubview(backImageView)
       contentView.addSubview(startBtn)
        
        //添加约束.
        backImageView.snp_makeConstraints { (make) -> Void in
            make.edges .equalTo(contentView.snp_edges)
        }
        
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
        }

        //添加按钮点击事件
        
        startBtn.addTarget(self, action: "startBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
    }
    
    
    
    //MARK: 给按钮添加动画
    
    func startAnimation(){
    
        
        startBtn.hidden = false
        
        //给按钮添加动画
        
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(2.0, animations: { () -> Void in
            
            self.startBtn.transform = CGAffineTransformIdentity
        })
        

    
    }
    
    
    
    //MARK:startBtn点击事件
    func startBtnClick(){
    
        //发送通知,跳转到首页
    NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: nil)
    }
    

    
    
    

    //懒加载子视图
    
    lazy var backImageView:UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    lazy var startBtn:UIButton = {
        
        let btn = UIButton()
        
         btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted) 
        
        btn.setTitle("立即体验", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.whiteColor(), forState:UIControlState.Normal)
        
        return btn
        }()



}











